package io.jee.runshan.controller.web;

import io.jee.medusa.vo.ResultBean;
import io.jee.runshan.domain.Product;
import io.jee.runshan.domain.ShoppingCar;
import io.jee.runshan.domain.ShoppingCarProduct;
import io.jee.runshan.service.IProductService;
import io.jee.runshan.service.IShoppingCarProductService;
import io.jee.runshan.service.IShoppingCarService;
import io.jee.runshan.utils.WeixinUtils;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 购物车Controller
 * 
 * @author ynshun
 *
 */
@Controller
@RequestMapping("/web/car/")
public class WebShoppingCarController {

	@Resource
	private IShoppingCarService carService;
	@Resource
	private IShoppingCarProductService carProductService;
	@Resource
	private IProductService productService;

	
	/**
	 * 获取购物车基本信息
	 * @author ynshun
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/detail", produces = { "application/json" })
	@ResponseBody
	public ResultBean<ShoppingCar> detail(HttpServletRequest request) {
		ShoppingCar car = null;
		try {
			String wxOpenid = WeixinUtils.getOpenid(request);
			if (wxOpenid == null) {
				return ResultBean.result(false, "请使用润山东方百货公众号浏览");
			}
			car = carService.unique("wxOpenid", wxOpenid);
			if (car == null) {
				car = new ShoppingCar();
				car.setWxOpenid(wxOpenid);
				carService.save(car);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultBean.result(true, "获取购物车详情成功", car);
	}
	
	/**
	 * 获取购物车商品详情
	 * @author ynshun
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/products", produces = { "application/json" })
	@ResponseBody
	public ResultBean<List<ShoppingCarProduct>> products(HttpServletRequest request) {
		List<ShoppingCarProduct> products = null;
		try {
			String wxOpenid = WeixinUtils.getOpenid(request);
			if (wxOpenid == null) {
				return ResultBean.result(false, "请使用润山东方百货公众号浏览");
			}
			products = carProductService.list("car.wxOpenid", wxOpenid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultBean.result(true, "获取购物车详情成功", products);
	}
	
	/**
	 * 添加商品至购物车
	 *@author ynshun
	 * @param request
	 * @param productId
	 * @param count
	 * @return
	 */
	@RequestMapping(value = "/addProduct", produces = { "application/json" })
	@ResponseBody
	public ResultBean<?> addProduct(HttpServletRequest request, Integer productId, Integer count) {
		try {
			String wxOpenid = WeixinUtils.getOpenid(request);
			if (wxOpenid == null) {
				return ResultBean.result(false, "请从润山东方百货公众号进入");
			}
			return carService.addProduct(wxOpenid, productId, count);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBean.result(false, "添加商品至购物车失败");
		}
	}
	
	/**
	 * 删除购物车中商品
	 * @author qinling.zhu
	 * @param request
	 * @param productId
	 * @return
	 */
	@RequestMapping(value = "/removeProduct", produces = { "application/json" })
	@ResponseBody
	public ResultBean<?> removeProduct(HttpServletRequest request, Integer productId) {
		try {
			String wxOpenid = WeixinUtils.getOpenid(request);
			if (wxOpenid == null) {
				return ResultBean.result(false, "请从润山东方百货公众号进入");
			}
			return carService.removeProduct(wxOpenid, productId);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBean.result(false, "删除购物车中商品失败");
		}
	}
	
	/**
	 * 从购物车减少商品数量
	 * @author ynshun
	 * @param request
	 * @param productId
	 * @return
	 */
	@RequestMapping(value = "/reduceCount", produces = { "application/json" })
	@ResponseBody
	public ResultBean<?> reduceCount(HttpServletRequest request, Integer productId) {
		try {
			String wxOpenid = WeixinUtils.getOpenid(request);
			if (wxOpenid == null) {
				return ResultBean.result(false, "请从润山东方百货公众号进入");
			}
			Product product = productService.get(productId);
			if (product == null) {
				return ResultBean.result(false, "内部功能，非请勿入");
			}
			return carService.reduceCount(wxOpenid, productId);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBean.result(false, "减少商品数量失败");
		}
	}
	
	/**
	 * 从购物车增加商品数量
	 * @author ynshun
	 * @param request
	 * @param productId
	 * @return
	 */
	@RequestMapping(value = "/addCount", produces = { "application/json" })
	@ResponseBody
	public ResultBean<?> addCount(HttpServletRequest request, Integer productId) {
		try {
			String wxOpenid = WeixinUtils.getOpenid(request);
			if (wxOpenid == null) {
				return ResultBean.result(false, "请从润山东方百货公众号进入");
			}
			Product product = productService.get(productId);
			if (product == null) {
				return ResultBean.result(false, "内部功能，非请勿入");
			}
			return carService.addCount(wxOpenid, productId);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBean.result(false, "从购物车增加商品数量失败");
		}
	}
	
	/**
	 * 更改购物车中商品数量
	 * @author ynshun
	 * @param request
	 * @param productId
	 * @param count
	 * @return
	 */
	@RequestMapping(value = "/changeCount", produces = { "application/json" })
	@ResponseBody
	public ResultBean<?> changeCount(HttpServletRequest request, Integer productId, Integer count) {
		try {
			String wxOpenid = WeixinUtils.getOpenid(request);
			if (wxOpenid == null) {
				return ResultBean.result(false, "请从润山东方百货公众号进入");
			}
			Product product = productService.get(productId);
			if (product == null) {
				return ResultBean.result(false, "内部功能，非请勿入");
			}
			return carService.changeCount(wxOpenid, productId, count);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBean.result(false, "从购物车增加商品数量失败");
		}
	}

}
